package qs.openxt.libs.remoting;

import android.content.Context;
import android.os.Environment;
import android.widget.Toast;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;

import qs.openxt.libs.App;
import qs.openxt.libs.R;
import qs.openxt.libs.progress.DownloadDialogProgress;
import qs.openxt.libs.util.ActivityUtil;
import qs.openxt.libs.util.Utils;

/**
 * Created by 颖杰_2 on 2016/3/29.
 */
public class Download {
    private String uri = null;
    private Context context = null;
    private String filePath = null;
    protected OnActionListener listener = null;
    protected DownloadDialogProgress dialogProgress = null;

    public Download(Context context, String uri, OnActionListener l){
        this.uri = uri;
        this.context = context;
        this.listener = l;

        filePath = this.uri.substring(this.uri.lastIndexOf("/") + 1);
        String download = Utils.getExternalStorageDirectory() + "/" + App.PATH_NAME + "/download/";
        File folder = new File(download);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        filePath = download + filePath;

        dialogProgress = new DownloadDialogProgress(context);
    }

    public void doRequest(){
        if (!ActivityUtil.isNetworkAvailable(context)) {
            Toast.makeText(context, context.getString(R.string.isNetworkAvailable), Toast.LENGTH_SHORT).show();
            return;
        }
        dialogProgress.show();
        try{
            /*********XUtils 文件下载********/
            RequestParams params = new RequestParams(uri);
            //设置断点续传
            params.setAutoResume(true);
            params.setAutoRename(true);
            params.setSaveFilePath(filePath);
            x.http().get(params, new Callback.ProgressCallback<File>() {
                @Override
                public void onSuccess(File result) {
                    doSuccess(result.getAbsolutePath());
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    doError(ex.getMessage());
                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }

                @Override
                public void onWaiting() {

                }

                @Override
                public void onStarted() {

                }

                @Override
                public void onLoading(long total, long current, boolean isDownloading) {
                    onProgress((int) ((current / (float) total) * 100));
                }
            });
            /*********XUtils 文件下载********/
        }catch (Exception e){
            dialogProgress.close();
        }
    }
    public void onProgress(int progress) {
        dialogProgress.setProgress(progress);
    }

    public void doSuccess(String reVal) {
        dialogProgress.close();

        if (listener != null) {
            listener.onActionCompleted(reVal);
        }
    }

    public void doError(String msg) {
        dialogProgress.close();

        if (listener != null) {
            listener.onActionError(msg);
        }
    }

    public abstract interface OnActionListener {
        public abstract void onActionCompleted(String reVal);
        public abstract void onActionError(String error);
    }
}
